
package geomatic.android;

import org.geonames.WikipediaArticle;

import android.content.Context;
import android.view.View;
import android.widget.*;

public class WikipediaArticleView extends LinearLayout
{
	private WikipediaArticle article;
	private boolean expanded = false;
	private TextView compactView;
	private View expandedView;
	
	public WikipediaArticleView(Context context, WikipediaArticle a)
	{
		super(context);
	
		this.expanded = false;
		
		this.article = a;
		
		this.setOrientation(LinearLayout.VERTICAL);
		
		compactView = createCompact(this.article);
		
		expandedView = createExpanded(this.article);
		expandedView.setVisibility(View.GONE);
		
		this.addView(compactView);
		this.addView(expandedView);
		
	}

	public void toggle()
	{
		setExpanded(!isExpanded());
	}
	
	protected TextView createCompact(WikipediaArticle a)
	{
		TextView tview = new TextView(this.getContext());
		tview.setText(a.getTitle());
		return tview;
	}
	
	protected View createExpanded(WikipediaArticle a)
	{
		
		TextView text = new TextView(this.getContext());
		text.setText(this.article.getTitle()
					+ "\n"
					+ this.article.getSummary());
		
		
		return text;
		
	}
	
	public void setExpanded(boolean b)
	{
		if (this.expanded == b)
		{
			return;
		}
		
		this.expanded = b;
		
		if (this.expanded)
		{
			this.compactView.setVisibility(View.GONE);
			this.expandedView.setVisibility(View.VISIBLE);
		}
		else
		{
			this.expandedView.setVisibility(View.GONE);
			this.compactView.setVisibility(View.VISIBLE);
		}
		// (?) this.requestLayout();
	}
	
	public boolean isExpanded()
	{
		return this.expanded;
	}
	
	public WikipediaArticle getWikipediaArticle()
	{
		return this.article;
	}
	
}
